package com.ai.service;

import com.ai.bo.QuestionLibBo;
import com.ai.enums.YesOrNo;
import com.ai.mapper.CandidateMapper;
import com.ai.mapper.JobMapper;
import com.ai.mapper.QuestionLibMapper;
import com.ai.service.impl.QuestionLibServiceImpl;
import com.ai.vo.InitQuestionVO;
import com.ai.vo.QuestionLibVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.micrometer.common.util.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wst.ai.pojo.question.QuestionLib;

import java.time.LocalDateTime;
import java.util.*;

@Service
public class QuestionLibService implements QuestionLibServiceImpl {
    @Autowired
    private QuestionLibMapper questionLibMapper;
    @Autowired
    private CandidateMapper candidateMapper;
    @Autowired
    private JobMapper jobMapper;

    @Override
    public void createOrUpdate(QuestionLibBo questionLibBo) {
        QuestionLib questionLib = new QuestionLib();
        BeanUtils.copyProperties(questionLibBo,questionLib);
        questionLib.setId(questionLibBo.getQuestionLibId());
        questionLib.setUpdatedTime(LocalDateTime.now());
        if (StringUtils.isBlank(questionLib.getId())){
            questionLib.setIsOn(YesOrNo.YES.type);
            questionLib.setCreateTime(LocalDateTime.now());
            questionLibMapper.insert(questionLib);
        }else {
            questionLibMapper.updateById(questionLib);
        }
    }

    @Override
    public PageInfo<QuestionLibVo> QuestionLibList(Map<String,Object> paramsMap, Integer page, Integer size) {
        PageHelper.startPage(page,  size);
        List<QuestionLibVo> questionLibVos = questionLibMapper.QuestionLibList(paramsMap);
// 用PageInfo封装结果（含总页数、当前页等）
        PageInfo<QuestionLibVo> pageInfo = new PageInfo<>(questionLibVos);
        return pageInfo;
    }

    /**
     * 启用禁用题目
     * @param questionLibId
     * @param isOn
     */
    @Override
    public void setDisplayOrNo(String questionLibId, Integer isOn) {
        QuestionLib questionLib = new QuestionLib();
        questionLib.setId(questionLibId);
        questionLib.setIsOn(isOn);
        questionLib.setUpdatedTime(LocalDateTime.now());
        questionLibMapper.updateById(questionLib);
    }

    @Override
    public void delete(String questionLibId) {
        questionLibMapper.deleteById(questionLibId);
    }

    /**
     * 随机抽取面试题
     * @param candidateId
     * @param questionNum
     * @return
     */
    @Override
    public List<InitQuestionVO> getRandomQuestion(String candidateId, Integer questionNum) {
        String jobId = candidateMapper.selectById(candidateId).getJobId();
        String interviewerId = jobMapper.selectById(jobId).getInterviewerId();
        Long questionCounts = questionLibMapper.selectCount(new QueryWrapper<QuestionLib>().eq("interviewer_id", interviewerId));
//        存储随机出来的题号
        ArrayList<Long> randomList = new ArrayList<>();
//        抽取题号
        Random random = new Random();
        for (int i = 0; i < questionNum; i++) {
            long randomNum = random.nextLong(questionCounts);
//        检查该题号有没有被抽取过
            if (randomList.contains(randomNum)){
                i--;//本次抽取无效,重置循环索引
                continue;
            }else {
                randomList.add(randomNum);
            }
        }
//        按照随机好的题号从题库里抽题
        LinkedList<InitQuestionVO> questionList = new LinkedList<>();
//        面试题
        List<QuestionLib> questionLibList = questionLibMapper.selectList(null);
        for (Long i:randomList) {
            InitQuestionVO initQuestionVO = getQuestion(i, questionLibList);
            questionList.add(initQuestionVO);
        }
        System.out.println("****questionList****"+questionList);
        return questionList;
    }

    @Override
    public InitQuestionVO getQuestion(Long i, List<QuestionLib> questionLibList) {
        QuestionLib question = questionLibList.get(Math.toIntExact(i));
        InitQuestionVO initQuestionVO = new InitQuestionVO();
        BeanUtils.copyProperties(question,initQuestionVO);
        initQuestionVO.setQuestionLibId(question.getId());
        return initQuestionVO;
    }
}
//    HashMap<String, Object> map = new HashMap<>();
//            map.put("indexNum",i);